package com.service.ljl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.ljl.orderdao;
import com.dao.ljl.ordermybatis;
import com.dao.luo.CusDao;
import com.entity.Customer;
import com.entity.Order;
import com.entity.Orderdetail;
import com.entity.User;

@Service
@Transactional(rollbackFor = ServiceException.class)
public class orderservice {
	@Autowired
	private orderdao orderdao;
	@Autowired
	private ordermybatis ordermybatis;
<<<<<<< HEAD

	@Autowired
	private CusDao cusDao;

	/**
	 * @param order
	 *            新建/更新订单合同
=======
	
	/**
	 * @param order
	 * 新建/更新订单合同
>>>>>>> refs/remotes/origin/lx
	 */
	public void add(Map map) {
		Order order = (Order) JSONObject.parseObject(map.get("order").toString(), Order.class);
		Long cusno = JSONObject.parseObject(map.get("cusno").toString(), Long.class);
		if (order.getOrTitle() == null)
			order.setOrTitle("无主题");
		order.setCustomer(cusDao.findOne(cusno));
		orderdao.save(order);
	}
<<<<<<< HEAD

	public List<Order> findallorder() {
		List<Order> orders = ordermybatis.findallorder();
		return orders;
	}

	/**
	 * @return 查询所有订单/合同
=======
	
	/**
	 * @return
	 * 分页查询订单合同
>>>>>>> refs/remotes/origin/lx
	 */
	public Map<Object, Object> findallorderright(int pagesize, int currage) {
		List<Order> orders = ordermybatis.findallorderright();
		Map<Object, Object> map = new HashMap<>();
		int start = (currage - 1) * pagesize;
		int finish = currage * pagesize;
		for (int i = 0; i < orders.size(); i++) {
			BigDecimal totol = new BigDecimal(0);
			BigDecimal billmoney = new BigDecimal(0);
			String state = null;
			int count = 0;
			int count1 = 0;
			if (orders.get(i).getPayplans() != null) {
				for (int z = 0; z < orders.get(i).getPayplans().size(); z++) {
					if (orders.get(i).getPayplans().get(z).getPayrecord() != null) {
						totol = totol.add(orders.get(i).getPayplans().get(z).getPpMoney());
						for (int x = 0; x < orders.get(i).getPayplans().get(z).getPayrecord().getBillrecords()
								.size(); x++) {
							billmoney = billmoney.add(ordermybatis.findbillmoney(orders.get(i).getPayplans().get(z)
									.getPayrecord().getBillrecords().get(x).getBrId()));
						}
					}
				}
			}
			List<BigDecimal> s = ordermybatis.findstate(orders.get(i).getOrId());
			for (int x = 0; x < s.size(); x++) {
				if (s.get(x).compareTo(new BigDecimal(1)) == 0) {
					count1++;
				} else if (s.get(x).compareTo(new BigDecimal(2)) == 0 || s.get(x).compareTo(new BigDecimal(3)) == 0) {
					count++;
				}
			}
			if (orders.get(i).getOrOr().compareTo(new BigDecimal(0)) == 0) {
				state = "---";
			} else if (count1 != 0) {
				state = "需发货";
			} else if (count == 0) {
				state = "未出库";
			} else if (count == s.size()) {
				state = "全部";
			} else {
				state = "部分";
			}

			orders.get(i).setState(state);
			orders.get(i).setReturnmoney(totol);
			orders.get(i).setBillmoney(billmoney);
		}
		if (finish > orders.size()) {
			finish = orders.size();
		}
		map.put("totol", orders.size());
		map.put("orders", orders.subList(start, finish));
		return map;
	}

	public List<Orderdetail> finddetail() {
		return ordermybatis.findallorderdetail();
	}

	/**
	 * @param id
	 * @return 查询开票金额
	 */
	public BigDecimal findbillmoney(Long id) {
		return ordermybatis.findbillmoney(id);
	}

	public List<BigDecimal> findstate(Long id) {
		return ordermybatis.findstate(id);
	}

	/**
	 * @param id
	 *            删除合同/订单
	 */
	public void delete(Long id) {
		orderdao.delete(id);
	}

	public List<Customer> findcustomer() {
		List<Customer> li = ordermybatis.findcustomer();
		// if(li.size()>10) {
		// li=li.subList(0, 10);
		// }
		return li;
	}

	public List<User> findowner() {
		return ordermybatis.findowner();
	}

	public void deleteorders(List<Long> orids) {
		for (int i = 0; i < orids.size(); i++) {
			orderdao.delete(orids.get(i));
		}
	}

	public Order findoneorder(Long id) {
		Order orders= ordermybatis.findone(id);
		BigDecimal totol = new BigDecimal(0);
		BigDecimal billmoney = new BigDecimal(0);
		String state = null;
		int count = 0;
		int count1 = 0;
		if (orders.getPayplans() != null) {
			for (int z = 0; z < orders.getPayplans().size(); z++) {
				if (orders.getPayplans().get(z).getPayrecord() != null) {
					totol = totol.add(orders.getPayplans().get(z).getPpMoney());
					for (int x = 0; x < orders.getPayplans().get(z).getPayrecord().getBillrecords()
							.size(); x++) {
						billmoney = billmoney.add(ordermybatis.findbillmoney(orders.getPayplans().get(z)
								.getPayrecord().getBillrecords().get(x).getBrId()));
					}
				}
			}
		}
		List<BigDecimal> s = ordermybatis.findstate(orders.getOrId());
		for (int x = 0; x < s.size(); x++) {
			if (s.get(x).compareTo(new BigDecimal(1)) == 0) {
				count1++;
			} else if (s.get(x).compareTo(new BigDecimal(2)) == 0 || s.get(x).compareTo(new BigDecimal(3)) == 0) {
				count++;
			}
		}
		if (orders.getOrOr().compareTo(new BigDecimal(0)) == 0) {
			state = "---";
		} else if (count1 != 0) {
			state = "需发货";
		} else if (count == 0) {
			state = "未出库";
		} else if (count == s.size()) {
			state = "全部";
		} else {
			state = "部分";
		}
		orders.setState(state);
		orders.setReturnmoney(totol);
		orders.setBillmoney(billmoney);
		return orders;
	}
	

}
